<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\BankInfoService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;

/**
 * Class BankInfoController
 * @package App\Controller
 *
 * @Controller(prefix="/admin/api/bankInfo")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class BankInfoController extends AdminBaseController
{

    /**
     * @Inject()
     * @var BankInfoService
     */
    protected $BankInfoService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 详情
     *
     * @PostMapping(path = "get")
     */
    public function get (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->BankInfoService->get($param);
    }

    /**
     * 过滤添加/编辑参数
     *
     * @param RequestInterface $request
     *
     * @return array
     */
    protected function _filterEditParam (RequestInterface $request) {

        $param = [
            'bankName'  => $request->post('bankName'),
            'bankNo'    => $request->post('bankNo'),
            'type'      => $request->post('type'),
            'host'      => $request->post('host'),
            'port'      => $request->post('port', 80),
            'isProxy'   => $request->post('isProxy', false),
            'proxyHost' => $request->post('proxyHost'),
            'proxyPort' => $request->post('proxyPort'),
        ];

        return $param;
    }

    /**
     * 添加
     *
     * @PostMapping(path = "add")
     */
    public function add (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'bankName' => 'required',
            // 'bankNo'   => 'required',
            'type'     => 'required',
            'host'     => 'required',
            'isProxy'  => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->BankInfoService->add($param);
    }

    /**
     * 编辑
     * @PostMapping(path = "edit")
     */
    public function edit(RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id'   => 'required',

            'bankName' => 'required',
            // 'bankNo'   => 'required',
            'type'     => 'required',
            'host'     => 'required',
            'isProxy'  => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $param['id'] = $request->post('id');

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->BankInfoService->edit($param);
    }

    /**
     * 删除
     * @PostMapping(path = "del")
     */
    public function del (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->BankInfoService->del($param);
    }

    /**
     * 列表
     *
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $param = [
            'page' => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'bankNo');
        CustomUtils::xzxAddSearch($param, $search, 'bankName');

        return $this->BankInfoService->list($param);
    }

    /**
     * 列表-下拉框
     *
     * @PostMapping(path = "options")
     */
    public function options (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            // 'search' => 'required',
        ]);

        $param = [
            'search' => $request->post('search'),
        ];

        return $this->BankInfoService->options($param);
    }

    /**
     * 银行类型-下拉框
     *
     * @PostMapping(path = "typeOptions")
     */
    public function typeOptions (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            // 'search' => 'required',
        ]);

        $param = [];

        return $this->BankInfoService->typeOptions($param);
    }

    /**
     * 该银行需要手动配置的参数-下拉框
     *
     * @PostMapping(path = "paramOptions")
     */
    public function paramOptions (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->BankInfoService->paramOptions($param);
    }

    /**
     * 该银行需要手动配置的参数-下拉框
     *
     * @PostMapping(path = "getInfoLog")
     */
    public function getInfoLog (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
        ]);

        $param = [
            'page' => $request->post('page', 1),
            'limit' => $request->post('limit', 20),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'bankId');

        return $this->BankInfoService->getInfoLog($param);
    }

}
